package com.mew.common.util;

import com.alibaba.fastjson.JSON;

import java.io.*;
import java.util.HashMap;

/**
 *
 * 短信功能
 */
public class SmsSDKUtil {


    public static boolean sendSMS(String mobile,String authCode,int time,String url,String apikey) {
        String code = "您的验证码是"+authCode+"，在"+time+"分钟内输入有效。如非本人操作请忽略此短信。";
        try {
            return sendNoteSMS(mobile, code,url,apikey);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     *
     * @description 单条发送
     *
     * 电话 验证码 失效时间
     * true 发送成功
     */
    public static boolean sendNoteSMS(String mobile, String noteMSG, String url, String apikey) throws UnsupportedEncodingException {

        //编码
        noteMSG = java.net.URLEncoder.encode(noteMSG, "GBK");
        HashMap<String, String> parameters = new HashMap<>();

        parameters.put("apikey",apikey);
        parameters.put("mobile",mobile );
        parameters.put("content",noteMSG );

        //转json
        String json = JSON.toJSONString(parameters);

        //发送
        String result = sendPostGBK(url, json);

        HashMap<String,Integer> map = JSON.parseObject(result, HashMap.class);
        String res = map.get("result")+"";
        if ("0".equals(res)){
            return true;
        }
        return false;
    }

    private static String sendPostGBK(String url, String json) {
        String result = "";// 返回的结果
        BufferedReader in = null;// 读取响应输入流
        PrintWriter out = null;
        StringBuffer sb = new StringBuffer();// 处理请求参数
        String params = "";// 编码之后的参数
        try {
            // 编码请求参数

            params = json;
            // 创建URL对象
            java.net.URL connURL = new java.net.URL(url);
            // 打开URL连接
            java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connURL
                    .openConnection();
            // 设置通用属性
            httpConn.setRequestProperty("POST", "/sms/v2/std/single_send HTTP/1.1");
            httpConn.setRequestProperty("Content-Type", "application/json");
            httpConn.setRequestProperty("Content-Length", "length");
            // 设置POST方式
            httpConn.setDoInput(true);
            httpConn.setDoOutput(true);
            // 获取HttpURLConnection对象对应的输出流
            out = new PrintWriter(httpConn.getOutputStream());
            // 发送请求参数
            out.write(params);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应，设置编码方式
            in = new BufferedReader(new InputStreamReader(httpConn
                    .getInputStream(), "UTF-8"));
            String line;
            // 读取返回的内容
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }


//    @Test
//    public void test() throws Exception{
//        boolean b = sendSMS("13881885441", "1234", 5, "http://api01.monyun.cn:7901/sms/v2/std/single_send", "1cc03941f661da068715fdf379506793");
//        System.out.println(b);
//    }

}
